<?php
namespace WXAPI\Controller;

use Think\Page;

class ArticleController extends BaseController {
 
    /***
     * 导航列表
     */
    public function getnavLists(){


        $navList = M('article_cat')->where(array('show_in_nav'=>1))->order('sort_order ')->select();

        $data = array('code'=>0,'msg'=>'获取成功','list'=>$navList);
        $json_arr = $data;
        
        exit(json_encode($json_arr));
    }

    /**
     * 文章列表
     */
    public function getarticleLists(){
         $type = I('type') ? I('type') : '';
         $p = I('page') ? I('page') : 0;
         if ($type){
             $articleLists = M('article')->where(array('is_open'=>1,'cat_id'=>$type))->select();
             $count = count($articleLists);
             $page = new Page($count,10);
             $page->firstRow = $p * $page->listRows;
             $articleList = M('article')->where(array('is_open'=>1,'cat_id'=>$type))->limit($page->firstRow.','.$page->listRows)->select();
         }else{
             $articleLists = M('article')->where(array('is_open'=>1))->select();
             $count = count($articleLists);
             $page = new Page($count,10);
             $page->firstRow = $p * $page->listRows;
             $articleList = M('article')->where(array('is_open'=>1))->limit($page->firstRow.','.$page->listRows)->select();
         }
        foreach ( $articleList as $k=>$v){
            $articleList[$k]['layout'] =1;
            $articleList[$k]['thumb'] = SITE_URL.$v['thumb'];
			
        }

        $data = array('code'=>0,'msg'=>'获取成功','list'=>$articleList);
        $json_arr = $data;
        exit(json_encode($json_arr));
    }

    /**
     *根据id获取指定页面的id
     */
    public function getarticleId(){
        $article_id =I('id','');
        $where = array('article_id'=>$article_id);
        //更新文章的浏览次数
        $Model = M('article');
        $click = $Model->field('click')->where($where)->select();
        $click =$click[0]['click'] + 1;
        $res = $Model->where($where)->save(array('click'=>$click));
        $articleList = $Model->where($where)->select();
        foreach ( $articleList as $k=>$v){
            $articleList[$k]['add_time'] =date('Y-m-d',$v['add_time']);
            $articleList[$k]['content'] = htmlspecialchars_decode($v['content']);
			$articleList[$k]['content'] = str_replace('/Public/upload/', SITE_URL."/Public/upload/", $v['content']);

        }
        $data = array('code'=>0,'msg'=>'获取成功','data'=>$articleList[0]);
        $json_arr =$data;
        exit(json_encode($json_arr));
    }


}